package com.atguigu.spzx.manager.service.Impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.service.exception.GuliException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SysRoleServiceImpl
 * Package: com.atguigu.spzx.manager.service.Impl
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/1 16:30
 * @Version 1.0
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysRole> sysRoleList =  sysRoleMapper.findByPage(sysRoleDto);
        return new PageInfo<>(sysRoleList);
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.sysRoleMapper(sysRole);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        if(sysRole.getId() == null){
            throw new GuliException(209,"修改失败,该用户不存在");
        }
        sysRoleMapper.updateSysRole(sysRole);
    }

    @Override
    public void deleteSysRoleById(Long id) {
        if(id==null){
            throw new GuliException(209,"删除失败,该用户不存在");
        }
        sysRoleMapper.deleteSysRoleById(id);
    }

    /**
     * 查询所有角色
     * @return
     */
    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        // 1. 查询所有角色
         List<SysRole> sysRoleList = sysRoleMapper.findAllRoles();
        // 2. 查询该用户已经拥有的角色的ID
        List<Long> sysRoleHasIds = sysRoleMapper.findByUserId(userId);
        // 3. 封装返回值
        HashMap<String, Object> map = new HashMap<>();
        map.put("allRolesList",sysRoleList);
        map.put("sysUserRoles",sysRoleHasIds);
        return map;
    }
}
